/*
EvoGen, Evolutionary Geneura is a framework for simulating distributed evolutionary computation experiments
Copyright (C) 2008 Junta de Andalucia CICE project P06-TIC-02025

This file is part of EvoGen.

EvoGen is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

EvoGen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with EvoGen; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

contact:  http://geneura.ugr.es, https://forja.rediris.es/svn/geneura/evogen
*/
package mirabilis.org.simplices;

import random.CommonState;
import mirabilis.org.config.Configuration;
import mirabilis.org.util.Initialization;


public class Coordinates{

	private double[] _coordinates;
	
	//TODO: [SIMPLEX] Initialize with simplex V/2
	public Coordinates(int length) {
		_coordinates = new double [length];
		for(int i=0; i<length; i++){
			_coordinates[i] = (CommonState.r.nextDouble()*(Configuration.upper_bound-Configuration.lower_bound))+Configuration.lower_bound;
		}
	}
	
	public Coordinates(double[] coordinates){
		_coordinates = new double [coordinates.length];
		for(int i=0; i<coordinates.length; i++){
			_coordinates[i] = coordinates[i];
		}
	}
	

	public int getDimensions() {
		return _coordinates.length;
	}

	
	public void setValueAtPosition(int pos, Object gen) {
		_coordinates[pos] = ((Double)gen).doubleValue();
	}


	public Object getValueAtPosition(int pos) {
		return new Double(_coordinates[pos]);
	}


	public double getValueAtPositionAsDouble(int pos) {
		return _coordinates[pos];
	}


	public double[] getCoordinatesAsDouble() {
		return _coordinates.clone();
	}
	
	public double[] getCoordinatesInTorusAsDouble(){
		return Initialization.toroidal_coordinates(_coordinates, Configuration.bounds);
	}

	
	public Object clone(){
		Coordinates bin = new Coordinates(this.getDimensions());
		for (int i=0;i<this.getDimensions();i++){
			bin._coordinates[i] = this._coordinates[i];
		}
		return bin;
	}
	
	public boolean equals(Object obj) {
		if (obj instanceof Coordinates) {
			Coordinates chr = (Coordinates) obj;
			if(chr.getDimensions() == this.getDimensions()){
				for(int i=0;i<this.getDimensions();i++)
					if(chr._coordinates[i] != this._coordinates[i])
					 return false;
	
			}else{
				return false;
			}
			
		}else{
			return false;
		}
		return true;
	}
	
	public String asString(){
		String a="";
		for(int i=0;i<this.getDimensions();i++){
			a += " "+_coordinates[i];
		}
		return a;
	}

	public String asStringOfTorus(){
		
		double[] toruscoord = getCoordinatesInTorusAsDouble();
		String a="";
		for(int i=0;i<this.getDimensions();i++){
			a += " "+toruscoord[i];
		}
		return a;
	}


}
